import { createRouter, createWebHistory, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
import Home from '../views/Home.vue';
import Order from '../views/Order.vue';
import Delivery from '../views/Delivery.vue';
import Profile from '../views/Profile.vue';
import Login from '../views/Login.vue';
import Reg from '../views/Reg.vue';
import address from '../views/address.vue';
import message from '../views/Myother/message.vue';
import Translation from '../views/Myother/Translation.vue';
import About from '../views/Myother/About.vue';
import member from '../views/Myother/member.vue';
import Integral from '../views/Myother/Integral.vue';
import Coupons from '../views/Myother/Coupons.vue';
import setone from '../views/setone.vue'; // 已移除 @ts-ignore
import deleteitem from '../views/backend/deleteitem.vue'; // 确保路径正确
import Updataitem from '../views/backend/updataitem.vue'; // 添加缺失的导入
import insteritem from '../views/backend/insteritem.vue'; // 添加缺失的导入
import settlement from '../views/shopping/settlement.vue';
import finish from '../views/shopping/finish.vue';
import over from '../views/shopping/over.vue';
import payment from '../views/shopping/payment.vue';
import appraise from '../views/shopping/appraise.vue';
import detail from '../views/shopping/detail.vue';
import Search from '../views/Search/Search.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'Home', component: Home, meta: { requiresAuth: true } },
    { path: '/order', name: 'order', component: Order, meta: { requiresAuth: true } },
    { path: '/delivery', name: 'Delivery', component: Delivery, meta: { requiresAuth: true } },
    { path: '/profile', name: 'Profile', component: Profile, meta: { requiresAuth: true } },
    { path: '/login', name: 'Login', component: Login },
    { path: '/reg', name: 'reg', component: Reg },
    { path: '/address', name: 'address', component: address },
    { path: '/setone', name: 'set', component: setone },
    { path: '/Myother/message', name: 'message', component: message },
    { path: '/Myother/Translation', name: 'Translation', component: Translation },
    { path: '/Myother/About', name: 'About', component: About },
    { path: '/Myother/member', name: 'member', component: member },
    { path: '/Myother/Integral', name: 'Integral', component: Integral },
    { path: '/Myother/Coupons', name: 'Coupons', component: Coupons },
    { path: '/backend/deleteitem', name: 'deleteitem', component: deleteitem },
    { path: '/backend/Updataitem', name: 'Updataitem', component: Updataitem },
    { path: '/backend/insteritem', name: 'insteritem', component: insteritem },
    { path: '/shopping/settlement', name: 'settlement', component: settlement },
    { path: '/shopping/finish', name: 'finish', component: finish },
    { path: '/shopping/over', name: 'over', component: over },
    { path: '/shopping/payment', name: 'payment', component: payment },
    { path: '/shopping/appraise', name: 'appraise', component: appraise },
    { path: '/shopping/detail/:id', name: 'detail', component: detail },
    { path: '/Search/Search', name: 'Search', component: Search },
  ]
});

// 全局前置守卫
router.beforeEach((to: RouteLocationNormalized, _from: RouteLocationNormalized, next: NavigationGuardNext) => {
  const isLoggedIn = sessionStorage.getItem('isLoggedIn') === 'true';

  if (to.path === '/login' && isLoggedIn) {
    next('/');
  } else if (to.meta.requiresAuth && !isLoggedIn) {
    next('/login');
  } else {
    next();
  }
});

export default router;